from django.shortcuts import render
from django.contrib.auth.models import User,Permission
from django.contrib.auth import authenticate,login,logout
from django.http import Http404
from rest_framework import viewsets
from rest_framework.decorators import detail_route, list_route,permission_classes
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework import status
from .models import*
from .serializer import*
# Create your views here.

class UserViewSet(viewsets.ModelViewSet):
    #queryset = User.objects.all()
    serializer_class = UserSerializer
    def get_queryset(self):
        ft = User.objects.filter(username = self.request.user)
        return ft

class PermissionViewSet(viewsets.ModelViewSet):
    queryset = Permission.objects.all()
    serializer_class = PermissionSerializer
    # pagination_class = LargeResultsSetPagination
    # def get_allPermission(self, request):
    #     permssions = Permission.objects.all()
    #     ft = [{'id':p.id,'name':p.name,'codename':p.codename} for p in permssions]
    #     return Response(ft)

class GroupViewSet(viewsets.ModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer

    @list_route()
    def getListGroup(self, request):
        groups = Group.objects.all()
        ft = [{'id':g.id,'name':g.name} for g in groups]
        # ft = GroupSerializer(groups)
        return Response(ft)

    def create(self, request):
        serialized = GroupSerializerNoDepth(data=request.data)
        if serialized.is_valid():
            serialized.save()
            return Response(serialized.data)
        else:
            return Response(serialized.errors)

    def update(self, request,pk=None):
        groups =  self.get_object() #Group.objects.get(pk = pk)
        serialized = GroupSerializerNoDepth(groups,data=request.data)
        if serialized.is_valid():
            serialized.save()
            serialized = self.serializer_class(groups)
            return Response(serialized.data)
        else:
            return Response(serialized.errors,status=status.HTTP_400_BAD_REQUEST)

    class SessionViewSet(viewsets.ModelViewSet):
        queryset = User.objects.all()
        serializer_class = UserSerializer
        permission_classes = (AllowAny,)

        # log in
        def create(self, request):
            username = request.data['username']
            password  = request.data['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                serializer = UserSerializer(user)
                content = {'status': 'ok','user':serializer.data}
                return Response(content)
            else:
                content = {'status': 'Username or password is wrong...'}
                return Response(content)

        # log out
        @list_route()
        def delete(self, request):
            logout(request)
            content = {'status': 'ok'}
            return Response(content)